package me.zhengjie.service.impl;

import me.zhengjie.mapper.CourseResultDao;
import me.zhengjie.pojo.*;
import me.zhengjie.service.CourseResultService;
import me.zhengjie.utils.StringUtils;
import net.dreamlu.mica.core.result.R;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 描述
 *
 * @Author xulitao
 * @Date 2022/3/7
 * @Version 1.0
 */
@Service
public class CourseResultServiceImpl implements CourseResultService {

    @Autowired
    @Qualifier("courseResultDao")
    private CourseResultDao courseResultDao;

    @Override
    public PageVo queryList(StuScoreVo stuScoreVo) {
        PageVo pageVo = new PageVo();
        if (null == stuScoreVo || StringUtils.isEmpty(stuScoreVo.getStuId())) {
            pageVo.setCode(500);
            pageVo.setMsg("PARAM ERROR");
            return pageVo;
        }

        Integer nums = courseResultDao.queryScoreNum(stuScoreVo);
        if (nums <= 0) {
            pageVo.setCode(500);
            pageVo.setMsg("QUERY NO DATA");
            return pageVo;
        }
        pageVo.setTotalNum(nums);
        pageVo.setCurrentPage(stuScoreVo.getPageIndex());
        pageVo.setPageSize(stuScoreVo.getPageSize());

        List<CourseResultEntity> entities = courseResultDao.queryList(stuScoreVo,
                                                                      (stuScoreVo.getPageIndex() - 1) * stuScoreVo.getPageSize(), stuScoreVo.getPageSize());

        if (CollectionUtils.isEmpty(entities)) {
            pageVo.setCode(500);
        } else {
            pageVo.setData(entities);
        }
        return pageVo;
    }

    @Override
    public List<StudentVo> queryStu() {
        return courseResultDao.queryStu();
    }

    @Override
    public ResultVo insertStuScore(List<CourseResultEntity> list) {
        if(CollectionUtils.isEmpty(list)){
            return new ResultVo("201","param error",null);
        }
        ResultVo resultVo =new ResultVo();

        // 根据学生id查询学生姓名

        courseResultDao.insertStuScore(list);
        resultVo.setCode("200");
        resultVo.setMgs("SUCCESS");


        return resultVo;
    }

    @Override
    public ResultVo getAllSubject() {
        ResultVo resultVo=new ResultVo();
        resultVo.setCode("200");
        resultVo.setMgs("SUCCESS");
        resultVo.setData(courseResultDao.getAllSubject());
        return resultVo;
    }

    @Override
    public ResultVo updateStuScore(CourseResultEntity entity) {
        ResultVo resultVo=new ResultVo();
        courseResultDao.updateStuScore(entity);
        resultVo.setCode("200");
        resultVo.setMgs("SUCCESS");
        return resultVo;
    }

    @Override
    public ResultVo delStuScore(CourseResultEntity entity) {
        ResultVo resultVo=new ResultVo();
        courseResultDao.delStuScore(entity);
        resultVo.setCode("200");
        resultVo.setMgs("SUCCESS");
        return resultVo;
    }

    @Override
    public ResultVo insertWorkFinish(List<WordFinishVo> list) {
        ResultVo resultVo=new ResultVo();
        courseResultDao.insertWorkFinish(list);
        resultVo.setCode("200");
        resultVo.setMgs("SUCCESS");
        return resultVo;
    }

    @Override
    public ResultVo updateWorkFinish(WordFinishVo entity) {
        ResultVo resultVo=new ResultVo();
        courseResultDao.updateWorkFinish(entity);
        resultVo.setCode("200");
        resultVo.setMgs("SUCCESS");
        return resultVo;
    }

    @Override
    public ResultVo delWorkFinish(WordFinishVo entity) {
        ResultVo resultVo=new ResultVo();
        courseResultDao.delWorkFinish(entity);
        resultVo.setCode("200");
        resultVo.setMgs("SUCCESS");
        return resultVo;
    }

    @Override
    public ResultVo insertPlayStation(List<AskAnswerVo> list) {
        ResultVo resultVo=new ResultVo();
        courseResultDao.insertPlayStation(list);
        resultVo.setCode("200");
        resultVo.setMgs("SUCCESS");
        return resultVo;
    }

    @Override
    public ResultVo updatePlayStation(AskAnswerVo entity) {
        ResultVo resultVo=new ResultVo();
        courseResultDao.updatePlayStation(entity);
        resultVo.setCode("200");
        resultVo.setMgs("SUCCESS");
        return resultVo;
    }

    @Override
    public ResultVo delPlayStation(AskAnswerVo entity) {
        ResultVo resultVo=new ResultVo();
        courseResultDao.delPlayStation(entity);
        resultVo.setCode("200");
        resultVo.setMgs("SUCCESS");
        return resultVo;
    }

    @Override
    public PageVo<AskAnswerEntity> getPlayStation(AskAnswerVo askAnswerVo) {
        PageVo<AskAnswerEntity> pageVo = new PageVo();
        if (null == askAnswerVo) {
            pageVo.setCode(500);
            pageVo.setMsg("PARAM ERROR");
            return pageVo;
        }

        Integer nums = courseResultDao.getPlayStationNUm(askAnswerVo);
        if (nums <= 0) {
            pageVo.setCode(500);
            pageVo.setMsg("QUERY NO DATA");
            return pageVo;
        }
        pageVo.setTotalNum(nums);
        pageVo.setCurrentPage(askAnswerVo.getPageIndex());
        pageVo.setPageSize(askAnswerVo.getPageSize());

        List<AskAnswerEntity> playStation = courseResultDao.getPlayStation(askAnswerVo,
                                                                           (askAnswerVo.getPageIndex() - 1) * askAnswerVo.getPageSize(), askAnswerVo.getPageSize());

        if (CollectionUtils.isEmpty(playStation)) {
            pageVo.setCode(500);
        } else {
            pageVo.setData(playStation);
        }
        return pageVo;
    }

    @Override
    public PageVo<WorkFinishEntity> getWorkFinish(WordFinishVo wordFinishVo) {
        PageVo<WorkFinishEntity> pageVo = new PageVo();
        if (null == wordFinishVo) {
            pageVo.setCode(500);
            pageVo.setMsg("PARAM ERROR");
            return pageVo;
        }

        Integer nums = courseResultDao.getWorkFinishNum(wordFinishVo);
        if (nums <= 0) {
            pageVo.setCode(500);
            pageVo.setMsg("QUERY NO DATA");
            return pageVo;
        }
        pageVo.setTotalNum(nums);
        pageVo.setCurrentPage(wordFinishVo.getPageIndex());
        pageVo.setPageSize(wordFinishVo.getPageSize());

        List<WorkFinishEntity> playStation = courseResultDao.getWorkFinish(wordFinishVo,
                                                                           (wordFinishVo.getPageIndex() - 1) * wordFinishVo.getPageSize(), wordFinishVo.getPageSize());

        if (CollectionUtils.isEmpty(playStation)) {
            pageVo.setCode(500);
        } else {
            pageVo.setData(playStation);
        }
        return pageVo;
    }

}
